package com.hnly.djdl.components

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hnly.djdl.R
import com.hnly.djdl.theme.MainTitleColor

@Composable
fun RotationAnimation() {
    val anim1 = remember { Animatable(0f) }
    val anim2 = remember { Animatable(0f) }
    val anim3 = remember { Animatable(0f) }
    val anim4 = remember { Animatable(0f) }
    //repeatable 设置重复次数
    LaunchedEffect(true) {
        anim1.animateTo(
            360f, animationSpec = infiniteRepeatable(animation = tween(durationMillis = 1000))
        )
    }
    LaunchedEffect(true) {
        anim2.animateTo(
            360f, animationSpec = infiniteRepeatable(animation = tween(durationMillis = 1000))
        )
    }
    LaunchedEffect(true) {
        anim3.animateTo(
            360f, animationSpec = infiniteRepeatable(animation = tween(durationMillis = 1000))
        )
    }
    LaunchedEffect(true) {
        anim4.animateTo(
            360f, animationSpec = infiniteRepeatable(animation = tween(durationMillis = 1000))
        )
    }

    Row(
        modifier = Modifier
            .padding(top = 90.dp, start = 65.dp, end = 65.dp)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            painter = painterResource(id = R.drawable.ic_tab_1),
            contentDescription = null,
            modifier = Modifier
                .size(40.dp)
                .graphicsLayer { rotationZ = anim1.value },
            contentScale = ContentScale.FillBounds,
            colorFilter = ColorFilter.tint(MainTitleColor)
        )
        Image(
            painter = painterResource(id = R.drawable.ic_tab_2),
            contentDescription = null,
            modifier = Modifier
                .size(40.dp)
                .graphicsLayer { rotationZ = anim2.value },
            contentScale = ContentScale.FillBounds,
            colorFilter = ColorFilter.tint(MainTitleColor)
        )
        Image(
            painter = painterResource(id = R.drawable.ic_tab_3),
            contentDescription = null,
            modifier = Modifier
                .size(40.dp)
                .graphicsLayer { rotationZ = anim3.value },
            contentScale = ContentScale.FillBounds,
            colorFilter = ColorFilter.tint(MainTitleColor)
        )
        Image(
            painter = painterResource(id = R.drawable.ic_tab_4),
            contentDescription = null,
            modifier = Modifier
                .size(40.dp)
                .graphicsLayer { rotationZ = anim4.value },
            contentScale = ContentScale.FillBounds,
            colorFilter = ColorFilter.tint(MainTitleColor)
        )
    }
}

@Preview
@Composable
fun RotationAnimationPreview() {
    Box(modifier = Modifier.fillMaxSize()) {
        RotationAnimation()
    }
}